package com.dliu.javadesign.behaviour.chain;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yanglei
 * date 2021/4/23 10:31
 */
@Slf4j
public class HandleChain {
    private final List<Handle> chain = new ArrayList<>();

    public HandleChain addHandle(Handle handle){
        chain.add(handle);
        return this;
    }

    public Boolean process(Sport sport){
        for (Handle handle : chain){
            Boolean process = handle.process(sport);
            if (process){
                return true;
            }
        }
        log.info("没有对应的handle");
        return false;
    }


    public static void main(String[] args) {
        HandleChain handleChain = new HandleChain().addHandle(new Liu())
                .addHandle(new Yang()).addHandle(new Yu());
        handleChain.process(new Sport().setName("swim"));
        handleChain.process(new Sport().setName("running"));
        handleChain.process(new Sport().setName("bike"));
    }
}
